package com.kylin.system.controller;

import com.kylin.common.core.annotation.NoApiInterceptor;
import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.core.entity.DataEntity;
import com.kylin.common.core.entity.Page;
import com.kylin.common.core.validator.Assert;
import com.kylin.common.core.validator.BeanValidator;
import com.kylin.common.log.annotation.Log;
import com.kylin.common.log.enums.BusinessType;
import com.kylin.common.poi.excel.EasyExcelUtil;
import com.kylin.system.entity.SysPost;
import com.kylin.system.service.SysPostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 岗位信息操作处理
 * 
 * @author wuhao
 */
@RestController
@RequestMapping("/post")
public class SysPostController
{
    @Autowired
    private SysPostService postService;

    /**
     * 获取岗位列表
     */
//    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public ResultBean<Page<SysPost>> list(SysPost post)
    {
        // 构造搜索
        post.getQueryWrapper()
                .and(StringUtils.isNotBlank(post.getSearchValue()),
                        q->q.like("post_code", post.getSearchValue())
                        .or()
                        .like("post_name", post.getSearchValue()))
                .eq(StringUtils.isNotBlank(post.getStatus()),"status", post.getStatus());

        Page<SysPost> page = new Page<>();
        page = postService.listPage(page, post);
        return ResultBean.onSuccess(page);
    }

    /**
     * 根据岗位编号获取详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public ResultBean<SysPost> getItem(@PathVariable Long postId)
    {
        // 非空校验
        Assert.isBlank("查询编号不能为空", postId);

        SysPost sysPost = postService.getById(postId);
        return ResultBean.onSuccess(sysPost);
    }


    /**
     * 新增岗位
     */
//    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultBean<Object> add(@RequestBody SysPost post)
    {
        // 参数校验
        BeanValidator.validate(post);
        postService.save(post);
        return ResultBean.onSuccess();
    }

    /**
     * 修改岗位
     */
//    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultBean<Object> edit(@Validated @RequestBody SysPost post)
    {
        postService.save(post);
        return ResultBean.onSuccess();
    }

    /**
     * 删除岗位
     */
//    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public ResultBean<Object> remove(@PathVariable Long[] postIds)
    {
        Assert.isBlank("请选择需要删除的数据" , postIds);
        int count = postService.deleteBatchIds(Arrays.asList(postIds));
        return ResultBean.onSuccess().put("count", count);
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public ResultBean<List<SysPost>> optionselect()
    {
        // 显示状态为正常的岗位
        SysPost sysPost = new SysPost();
        sysPost.setStatus(DataEntity.STATUS_NORMAL);
        List<SysPost> posts = postService.list(sysPost);
        return ResultBean.onSuccess(posts);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:post:export')")
    @PostMapping("/export")
    @NoApiInterceptor
    public void export(HttpServletResponse response, SysPost post) throws IOException
    {
        // 构造搜索
        post.getQueryWrapper()
                .and(StringUtils.isNotBlank(post.getSearchValue()),
                        q->q.like("post_code", post.getSearchValue())
                                .or()
                                .like("post_name", post.getSearchValue()))
                .eq(StringUtils.isNotBlank(post.getStatus()),"status", post.getStatus());
        List<SysPost> list = postService.list(post);
        EasyExcelUtil.exportExcel(list, "岗位数据汇总", "岗位数据", SysPost.class, "岗位数据导出", response);
    }






}
